package com.prs.crm.service.purchase.impl;

import java.util.List;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.purchase.PurchaseReturnedDao;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.purchase.FundsInBar;
import com.prs.crm.domain.purchase.PaymentOrder;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseOrderDetail;
import com.prs.crm.domain.purchase.PurchaseReturned;
import com.prs.crm.domain.purchase.PurchaseReturnedDetail;
import com.prs.crm.domain.purchase.PurchaseReturnedStoreOut;
import com.prs.crm.domain.purchase.PurchaseReturnedStoreOutDetail;
import com.prs.crm.domain.purchase.PurchaseStoreInDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.purchase.FundsInBarService;
import com.prs.crm.service.purchase.PaymentOrderService;
import com.prs.crm.service.purchase.PurchaseOrderDetailService;
import com.prs.crm.service.purchase.PurchaseOrderService;
import com.prs.crm.service.purchase.PurchaseReturnedService;
import com.prs.crm.service.purchase.PurchaseReturnedStoreOutDetailService;
import com.prs.crm.service.purchase.PurchaseReturnedStoreOutService;
import com.prs.crm.service.purchase.PurchaseStoreInDetailService;
import com.prs.crm.service.purchase.PurchaseStoreInService;
import com.prs.crm.service.stock.ProductService;

public class PurchaseReturnedServiceImpl extends
		FlowServiceImpl<PurchaseReturned, PurchaseReturnedDao> implements
		PurchaseReturnedService {

	private FundsInBar fundsInBar;

	private PurchaseReturnedStoreOut purchaseReturnedStoreOut;

	private PurchaseReturnedStoreOutDetail purchaseReturnedStoreOutDetail;

	private FundsInBarService fundsInBarService;
	private ProductService productService;
	private PurchaseOrderService purchaseOrderService;
	private PurchaseOrderDetailService purchaseOrderDetailService;

	private PurchaseReturnedStoreOutService purchaseReturnedStoreOutService;

	private PurchaseReturnedStoreOutDetailService purchaseReturnedStoreOutDetailService;
	private PurchaseStoreInService purchaseStoreInService;
	private PurchaseStoreInDetailService purchaseStoreInDetailService;
	private PaymentOrderService paymentOrderService;

	private double AllInMony = 0.0;

	@Override
	public boolean valideSubmit(PurchaseReturned entity) {

		return true;
	}

	@Override
	public void onProcessEnd(WorkFlowProcess process) {
		super.onProcessEnd(process);
		PurchaseReturned project = this.getProcessEntity(process);
		if (process.getResult() == Constrains.WORKFLOWPROCESS_RESULT_PASS) {
			boolean a = false;
			for (PurchaseReturnedDetail detail11 : project.getDetails()) {
				if (detail11.getDetailAll() != null
						&& !detail11.getDetailAll().trim().equals("")) {
					a = true;
					break;
				}
			}
			if (a = true) {
				PurchaseReturnedStoreOut PurchaseReturnedStoreOutObject = new PurchaseReturnedStoreOut();
				PurchaseReturnedStoreOutObject.setStoreRoom(project.getRooms());
				PurchaseReturnedStoreOutObject.setInState(0);
				PurchaseReturnedStoreOutObject.setReturnCode(project
						.getBillCode());
				PurchaseReturnedStoreOutObject
						.setBatchNum(project.getBtchNum());
				this.getPurchaseReturnedStoreOutService().save(
						PurchaseReturnedStoreOutObject);
				for (PurchaseReturnedDetail detail : project.getDetails()) {
					int count = 0;
					if (detail.getDetailAll() != null
							&& !detail.getDetailAll().trim().equals("")) {
						String[] all = detail.getDetailAll().trim().split(",");

						for (int c = 0; c < all.length; c++) {
							String[] val = all[c].trim().split("p");
							PurchaseStoreInDetail inDetail = this
									.getPurchaseStoreInDetailService().get(
											Integer.parseInt(val[1].trim()));
							AllInMony = AllInMony
									+ Integer.parseInt(val[0].trim())
									* inDetail.getPrice();
							this.getPurchaseStoreInDetailService().save(
									inDetail);
							count = count + Integer.parseInt(val[0].trim());

						}
					}
					if (count != 0) {
						PurchaseReturnedStoreOutDetail PurchaseReturnedStoreOutDetailOBject = new PurchaseReturnedStoreOutDetail();
						PurchaseReturnedStoreOutDetailOBject.setPrName(detail
								.getProductName());
						PurchaseReturnedStoreOutDetailOBject.setProduct(detail
								.getProduct());
						PurchaseReturnedStoreOutDetailOBject.setOutNum(count);
						PurchaseReturnedStoreOutDetailOBject
								.setDetailAlls(detail.getDetailAll());
						PurchaseReturnedStoreOutDetailOBject
								.setPurchaseReturnedStoreOut(PurchaseReturnedStoreOutObject);
						this.getPurchaseReturnedStoreOutDetailService().save(
								PurchaseReturnedStoreOutDetailOBject);
					}

				}

				FundsInBar FundsInBarObject = new FundsInBar();
				FundsInBarObject.setInMony(AllInMony);
				FundsInBarObject.setInState(0);
				FundsInBarObject.setReturnCode(project.getBillCode());
				FundsInBarObject.setPurchaseOrder(project.getOrder());
				project.setFundsInBar(fundsInBar);
				project
						.setPurchaseReturnedStoreOut(PurchaseReturnedStoreOutObject);
				this.getDao().save(project);
				this.getFundsInBarService().save(FundsInBarObject);
			}
			PurchaseOrder order = project.getOrder();
			if (order.getReturenState() == 1) {
				List<PaymentOrder> paymentOrders = this
						.getPaymentOrderService()
						.getPaymentOrdersByOrder(order);
				for (PaymentOrder order2 : paymentOrders) {
					order2.setPatState(3);
					this.getPaymentOrderService().save(order2);
				}
			}
		} else {
			PurchaseOrder purchaseOrder = project.getOrder();
			for (PurchaseReturnedDetail detail : project.getDetails()) {
				int count = 0;
				if (detail.getDetailAll() != null
						&& !detail.getDetailAll().trim().equals("")) {
					String[] all = detail.getDetailAll().split(",");

					for (int c = 0; c < all.length; c++) {

						String[] val = all[c].split("p");

						PurchaseStoreInDetail inDetail = this
								.getPurchaseStoreInDetailService().get(
										Integer.parseInt(val[1].trim()));
						inDetail.setOutNum(inDetail.getOutNum()
								- Integer.parseInt(val[0].trim()));
						count = count + Integer.parseInt(val[0].trim());
						this.getPurchaseStoreInDetailService().save(inDetail);
					}
				}
				for (PurchaseOrderDetail detail2 : purchaseOrder.getDetails()) {
					if (detail.getProduct() == detail2.getProduct()) {
						detail2.setReturnNum(detail2.getReturnNum()
								- detail.getNum());
						detail2.setInNum(detail2.getInNum() + count);
						detail2.setReturnState(0);
						purchaseOrderDetailService.save(detail2);
					}
				}
			}

			purchaseOrder.setReturenState(0);
			purchaseOrderService.save(purchaseOrder);

		}

		super.onProcessEnd(process);
	}

	@Override
	public void saveAll(PurchaseReturned purchaseReturned,
			String[] productCodes, String[] productNames, String[] units,
			Integer[] nums, Double[] prices, Double[] allPrices,
			Double[] rebates, Double[] rebateUnits, Double[] rebatePrices,
			String[] barCodes, Product[] products, String[] detailAll,
			Integer[] storeinNum) {

		PurchaseOrder purchaseOrder = purchaseReturned.getOrder();
		boolean returnstate = true;
		for (PurchaseOrderDetail detail : purchaseOrder.getDetails()) {
			for (int c = 0; c < products.length; c++) {
				if (products[c] == detail.getProduct()) {
					detail.setInNum(detail.getInNum() - storeinNum[c]);
					detail.setReturnNum(detail.getReturnNum() + nums[c]
							+ storeinNum[c]);
					if (detail.getStockNum() - detail.getReturnNum() == 0) {
						detail.setReturnState(1);

					}

				}

			}
			purchaseOrderDetailService.save(detail);
		}
		for (PurchaseOrderDetail detail : purchaseOrder.getDetails()) {
			if (detail.getReturnState() != 1) {
				returnstate = false;
				break;
			}
		}
		if (returnstate) {
			purchaseOrder.setReturenState(1);
		}
		purchaseOrderService.save(purchaseOrder);
		for (int i = 0; i < products.length; i++) {
			PurchaseReturnedDetail purchaseReturnedDetailN = new PurchaseReturnedDetail();
			purchaseReturnedDetailN.setProductCode(productCodes[i]);
			purchaseReturnedDetailN.setProductName(productNames[i]);
			purchaseReturnedDetailN.setBarCode(barCodes[i]);
			purchaseReturnedDetailN.setUnit(units[i]);
			purchaseReturnedDetailN.setNum(nums[i] + storeinNum[i]);
			purchaseReturnedDetailN.setPrice(prices[i]);
			purchaseReturnedDetailN.setProduct(products[i]);
			purchaseReturnedDetailN.setAllPrice(allPrices[i]);
			purchaseReturnedDetailN.setRebate(rebates[i]);
			purchaseReturnedDetailN.setRebateUnit(rebateUnits[i]);
			purchaseReturnedDetailN.setRebatePrice(rebatePrices[i]);
			purchaseReturnedDetailN.setDetailAll(detailAll[i]);
			purchaseReturned.addDetail(purchaseReturnedDetailN);

		}
		for (int i = 0; i < detailAll.length; i++) {
			if (detailAll[i] != null && !detailAll[i].trim().equals("")) {
				String[] all = detailAll[i].trim().split(",");
				for (int c = 0; c < all.length; c++) {
					String[] val = all[c].trim().split("p");
					PurchaseStoreInDetail inDetail = this
							.getPurchaseStoreInDetailService().get(
									Integer.parseInt(val[1].trim()));
					inDetail.setOutNum(inDetail.getOutNum()
							+ Integer.parseInt(val[0].trim()));
					this.getPurchaseStoreInDetailService().save(inDetail);
				}
			}
		}
		super.save(purchaseReturned);

	}

	public FundsInBar getFundsInBar() {
		return fundsInBar;
	}

	public void setFundsInBar(FundsInBar fundsInBar) {
		this.fundsInBar = fundsInBar;
	}

	public PurchaseReturnedStoreOut getPurchaseReturnedStoreOut() {
		return purchaseReturnedStoreOut;
	}

	public void setPurchaseReturnedStoreOut(
			PurchaseReturnedStoreOut purchaseReturnedStoreOut) {
		this.purchaseReturnedStoreOut = purchaseReturnedStoreOut;
	}

	public PurchaseReturnedStoreOutDetail getPurchaseReturnedStoreOutDetail() {
		return purchaseReturnedStoreOutDetail;
	}

	public void setPurchaseReturnedStoreOutDetail(
			PurchaseReturnedStoreOutDetail purchaseReturnedStoreOutDetail) {
		this.purchaseReturnedStoreOutDetail = purchaseReturnedStoreOutDetail;
	}

	public FundsInBarService getFundsInBarService() {
		return fundsInBarService;
	}

	public void setFundsInBarService(FundsInBarService fundsInBarService) {
		this.fundsInBarService = fundsInBarService;
	}

	public PurchaseReturnedStoreOutService getPurchaseReturnedStoreOutService() {
		return purchaseReturnedStoreOutService;
	}

	public void setPurchaseReturnedStoreOutService(
			PurchaseReturnedStoreOutService purchaseReturnedStoreOutService) {
		this.purchaseReturnedStoreOutService = purchaseReturnedStoreOutService;
	}

	public PurchaseReturnedStoreOutDetailService getPurchaseReturnedStoreOutDetailService() {
		return purchaseReturnedStoreOutDetailService;
	}

	public void setPurchaseReturnedStoreOutDetailService(
			PurchaseReturnedStoreOutDetailService purchaseReturnedStoreOutDetailService) {
		this.purchaseReturnedStoreOutDetailService = purchaseReturnedStoreOutDetailService;
	}

	@Override
	public FundsInBar getFundsInBar(String code) {

		return this.getDao().getFundsInBar(code);
	}

	@Override
	public PurchaseReturnedStoreOut getPurchaseReturnedStoreOut(String code) {

		return this.getDao().getPurchaseReturnedStoreOut(code);
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public PurchaseOrderDetailService getPurchaseOrderDetailService() {
		return purchaseOrderDetailService;
	}

	public void setPurchaseOrderDetailService(
			PurchaseOrderDetailService purchaseOrderDetailService) {
		this.purchaseOrderDetailService = purchaseOrderDetailService;
	}

	public PurchaseStoreInService getPurchaseStoreInService() {
		return purchaseStoreInService;
	}

	public void setPurchaseStoreInService(
			PurchaseStoreInService purchaseStoreInService) {
		this.purchaseStoreInService = purchaseStoreInService;
	}

	public PurchaseStoreInDetailService getPurchaseStoreInDetailService() {
		return purchaseStoreInDetailService;
	}

	public void setPurchaseStoreInDetailService(
			PurchaseStoreInDetailService purchaseStoreInDetailService) {
		this.purchaseStoreInDetailService = purchaseStoreInDetailService;
	}

	public PaymentOrderService getPaymentOrderService() {
		return paymentOrderService;
	}

	public void setPaymentOrderService(PaymentOrderService paymentOrderService) {
		this.paymentOrderService = paymentOrderService;
	}

}
